--
-- Author: mm
-- Date: 2019-05-15 16:17:15
--
-- CrossServerView
--
local CrossServerView = class("CrossServerView", app.mvc.ViewBase)
local TabsBtn = require("app.widget.TabsBtn")

local CrossServerGuildWarHelper = import(".guildWar.CrossServerGuildWarHelper")
local GuildWarPanel = import(".guildWar.panels.GuildWarPanel")
-- local EliteMatchPanel = import(".eliteMatch.panels.EliteMatchPanel")
-- local UniversalShopPanel = require("app.prestige.views.UniversalShopPanel")

CrossServerView.VIEW_ROOT_RES = Res.CrossServerMainScene

function CrossServerView:init()
	self.super.init(self)
	self._ui["Panel/Button_close"]:addClickEventListener(function()
		executeFunc(self._onCloseView)
	end)
	self:registerViewEvent({
        {name = ViewEvent.Common_RedPonitChange, callback = handler(self, self._onRefreshRedPoint)},
    })

end




function CrossServerView:_refreshRedPoint()
	for k, v in pairs(self._tabConfigs or {}) do
		if v.key == Enums.CrossServerTab.GuildWar then
			local redPoint = self._ui["Panel/Panel_tab/selected_btn_"..k.."/dot"]
			if redPoint then
				redPoint:setVisible(UD.redPoint:getStateByKey(Constant.FUNC_TYPE_CS_GUILD_WAR))
			end
			break
		end
	end
end

function CrossServerView:_onRefreshRedPoint(eventName, changeKeys)
	print(changeKeys, changeKeys[RedPointConstant.CSGuildWar_Challenge], "======>>>1111")
	dump(changeKeys)
    if changeKeys and changeKeys[Constant.FUNC_TYPE_CS_GUILD_WAR] ~= nil then
        self:_refreshRedPoint()
    end
end

function CrossServerView:initWithDefaultTab(defaultTabKey)
	if not self._tabConfigs then
		self:_initTabs(defaultTabKey)
	else
		self:setTabSelectedByKey(defaultTabKey)
	end
	self:_refreshRedPoint()
end

function CrossServerView:_initTabs(defaultTabKey)
	local tabConfigs = {}
	-- -- 精英赛页签
	-- if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CS_ELITEMATCH) then
	-- 	table.insert(tabConfigs, {key = Enums.CrossServerTab.Elite, name = L("CrossServerEliteTab"),panel = nil})
	-- end
	-- 工会战页签
	if CrossServerGuildWarHelper.isGuildWarFuncOpen() then
		table.insert(tabConfigs, {key = Enums.CrossServerTab.GuildWar, name = L("CrossServerGuildWarTab"), panel = nil})
	end
	-- -- 商店页签
	-- if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CS_SHOP) then
	-- 	table.insert(tabConfigs, {key = Enums.CrossServerTab.Shop, name = L("CrossServerShopTab"), panel = nil})
	-- end
	local tabNum = #tabConfigs
	local defaultTabIndex = 1
	self._tabConfigs = tabConfigs
	local btns = {}
	for i =1 , 4 do
		if i <= tabNum then
			local config = tabConfigs[i]

			if config.key == defaultTabKey then
				defaultTabIndex = i
			end

			local btn = self._ui["Panel/Panel_tab/selected_btn_"..i]
			local name1 = btn:getChildByName("name_1")
			local name2 = btn:getChildByName("name_2")
			name1:setString(config.name)
			name2:setString(config.name)
			table.insert(btns, btn)
		else
			self._ui["Panel/Panel_tab/selected_btn_"..i]:setVisible(false)
		end
	end
	self._cutSelectIndex = nil
	self._tabsBtn = TabsBtn.new(btns, handler(self, self._onTabsBtn), defaultTabIndex)
end

function CrossServerView:getPanelByKey(key, isForce)
	for k, v in pairs(self._tabConfigs) do
		if v.key == key then
			if not v.panel and isForce then
				v.panel = self._createPanelByKey(key)
			end
			return v.panel
		end
	end
end

function CrossServerView:setTabSelectedByKey(key)
	for k, v in pairs(self._tabConfigs) do
		if v.key == key then
			self._tabsBtn:setSelected(k, true)
			break
		end
	end
end


function CrossServerView:_createPanelByKey(key)
	local panel
	if key == Enums.CrossServerTab.Elite then
		-- 精英赛
		-- panel = EliteMatchPanel.create()
	elseif key == Enums.CrossServerTab.GuildWar then
		-- 跨服工会战
		panel = GuildWarPanel.create()
	elseif key == Enums.CrossServerTab.Shop then
		-- 商店 
		-- panel = UniversalShopPanel.create()
		-- panel:setShopId(72072)
		-- panel:updateLeftPanel()
	end
	if panel then
		self._ui["Panel/node"]:addChild(panel)
	end
	return panel
end

function CrossServerView:_onTabsBtn(index, tab)
	if self._cutSelectIndex == index then
		return
	end
	self._cutSelectIndex = index
	for i = 1, #self._tabConfigs do
		local tabConfig = self._tabConfigs[i]
		local panel = tabConfig.panel
		if i == index then
			if panel then
				panel:setVisible(true)
			else
				panel = self:_createPanelByKey(tabConfig.key)
				tabConfig.panel = panel
			end
			if panel and panel.onTabSelect then
                panel:onTabSelect()
            end
		else
			if panel then
				panel:setVisible(false)
			end
		end
	end
end

return CrossServerView
